package com.xxl.job.admin.repository;

import com.xxl.job.admin.core.conf.XxlJobAdminConfig;
import com.xxl.job.admin.core.enums.DatabaseTypeEnum;
import com.xxl.job.admin.core.model.XxlJobLog;
import com.xxl.job.admin.dao.XxlJobLogDao;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Repository
public class XxlJobLogRepository implements XxlJobLogDao {

    private final XxlJobLogDao xxlJobLogDao;

    public XxlJobLogRepository(XxlJobLogDao xxlJobLogDao) {
        this.xxlJobLogDao = xxlJobLogDao;
    }

    @Override
    public List<XxlJobLog> pageList(int offset, int pagesize, int jobGroup, int jobId, Date triggerTimeStart, Date triggerTimeEnd, int logStatus) {
        return xxlJobLogDao.pageList(offset, pagesize, jobGroup, jobId, triggerTimeStart, triggerTimeEnd, logStatus);
    }

    @Override
    public int pageListCount(int offset, int pagesize, int jobGroup, int jobId, Date triggerTimeStart, Date triggerTimeEnd, int logStatus) {
        return xxlJobLogDao.pageListCount(offset, pagesize, jobGroup, jobId, triggerTimeStart, triggerTimeEnd, logStatus);
    }

    @Override
    public XxlJobLog load(long id) {
        return xxlJobLogDao.load(id);
    }

    @Override
    public long save(XxlJobLog xxlJobLog) {
        DatabaseTypeEnum typeEnum = DatabaseTypeEnum.getByType(XxlJobAdminConfig.getAdminConfig().getDatabaseId());
        switch (typeEnum) {
            case MYSQL:
                return xxlJobLogDao.save(xxlJobLog);
            case ORACLE:
                // 从Oracle序列中获取ID并设值
                xxlJobLog.setId(xxlJobLogDao.getNextOracleSequenceId());
                return xxlJobLogDao.saveWithId(xxlJobLog);
            default:
                throw new RuntimeException("un support database type");
        }
    }

    @Override
    public long getNextOracleSequenceId() {
        return xxlJobLogDao.getNextOracleSequenceId();
    }

    @Override
    public long saveWithId(XxlJobLog xxlJobLog) {
        return xxlJobLogDao.saveWithId(xxlJobLog);
    }

    @Override
    public int updateTriggerInfo(XxlJobLog xxlJobLog) {
        return xxlJobLogDao.updateTriggerInfo(xxlJobLog);
    }

    @Override
    public int updateHandleInfo(XxlJobLog xxlJobLog) {
        return xxlJobLogDao.updateHandleInfo(xxlJobLog);
    }

    @Override
    public int delete(int jobId) {
        return xxlJobLogDao.delete(jobId);
    }

    @Override
    public Map<String, Object> findLogReport(Date from, Date to) {
        return xxlJobLogDao.findLogReport(from, to);
    }

    @Override
    public List<Long> findClearLogIds(int jobGroup, int jobId, Date clearBeforeTime, int clearBeforeNum, int pagesize) {
        return xxlJobLogDao.findClearLogIds(jobGroup, jobId, clearBeforeTime, clearBeforeNum, pagesize);
    }

    @Override
    public int clearLog(List<Long> logIds) {
        return xxlJobLogDao.clearLog(logIds);
    }

    @Override
    public List<Long> findFailJobLogIds(int pagesize) {
        return xxlJobLogDao.findFailJobLogIds(pagesize);
    }

    @Override
    public int updateAlarmStatus(long logId, int oldAlarmStatus, int newAlarmStatus) {
        return xxlJobLogDao.updateAlarmStatus(logId, oldAlarmStatus, newAlarmStatus);
    }

    @Override
    public List<Long> findLostJobIds(Date losedTime) {
        return xxlJobLogDao.findLostJobIds(losedTime);
    }
}
